

using System;
using System.Data.SqlClient;
using System.Data;
using System.Collections.Generic;
namespace GeneralClasses
{

    //larger container class that holds all of the data for a specific day

    public class DayOfWeek
    {

        //==============================================================================
        //                                                                     VARIABLES
        //==============================================================================

        #region variables

        int DOWID; // the ID from the database
        public const int numDays = 7;
        public enum DOW
        {
            Sunday = 0,
            Monday = 1,
            Tuesday = 2,
            Wednesday = 3,
            Thursday = 4,
            Friday = 5,
            Saturday = 6
        } DOW dow;

        DatabaseConnection dbc;

        #endregion

        #region properties


        public int getDOWID
        {
            get { return DOWID; }
            set { DOWID = value; }
        }

        public DOW getDOW
        {
            get { return dow; }
            set { dow = value; }
        }
        #endregion

        #region constructor

        public DayOfWeek()
        {
            DOWID = -1;
            dow = DOW.Sunday;
            dbc = new DatabaseConnection();
        }

        public DayOfWeek(int index)
        {
            DOWID = -1;

            dow = (DOW)(index % 7);
            dbc = new DatabaseConnection();
        }

        public DayOfWeek(String name)
        {
            DOWID = -1;
            setEnum(name.Trim());
            dbc = new DatabaseConnection();
        }

        #endregion


        #region private

        private void setEnum(String name)
        {
            if (name.CompareTo("Monday") == 0) { dow = DOW.Monday; }
            else if (name.CompareTo("Tuesday") == 0) { dow = DOW.Tuesday; }
            else if (name.CompareTo("Wednesday") == 0) { dow = DOW.Wednesday; }
            else if (name.CompareTo("Thursday") == 0) { dow = DOW.Thursday; }
            else if (name.CompareTo("Friday") == 0) { dow = DOW.Friday; }
            else if (name.CompareTo("Saturday") == 0) { dow = DOW.Saturday; }
            else { dow = DOW.Sunday; }
        }


        private int findID()
        {
            try
            {
                int id = -1;
                DataTable table = dbc.query("SELECT dowid, name FROM DayOfWeek");
                if ((table != null) && table.Rows.Count > 0)
                {
                    foreach (DataRow row in table.Rows)
                    {
                        if (((String)row["name"].ToString()).CompareTo(dow.ToString()) == 0)
                        {
                            id = (int)row["dowid"];
                        }
                    }
                }

                return id;
            }
            catch (Exception e)
            {
                UTIL.sayDebug(e);
                throw e;
            }
        }


        private bool isBeingUsed()
        {
            try
            {
                DataTable table = dbc.query("SELECT * FROM datehasdayofweekrelationship WHERE dowid='" + this.DOWID + "'");
                if ((table != null) && table.Rows.Count > 0)
                {
                    return true;
                }

            }
            catch (Exception e)
            {
                UTIL.sayDebug(e);
                throw e;
            }
            return false;

        }

        private bool isAlreadyIncluded()
        {
            try
            {
                DataTable table = dbc.query("SELECT * FROM DayOfWeek WHERE name = '" + this.dow.ToString() + "'");
                if ((table != null) && (table.Rows.Count > 0))
                {
                    return true;
                }
                return false;
            }
            catch (Exception e)
            {
                UTIL.sayDebug(e);
                throw e;
            }
        }

        #endregion

        #region public


        /// <summary>
        /// saves the DayOfWeek Data, if its redundant, it wont bother
        /// </summary>
        public int saveData()
        {
            try
            {
                if (this.DOWID == -1)
                {
                    if (!this.isAlreadyIncluded()) // ALREADY IN?
                    {
                        // ADD
                        char letter = this.dow.ToString().ToCharArray()[0];
                        dbc.executeNonQuery("INSERT INTO DayOfWeek (letter, name) values ('" + letter + "','" + this.dow.ToString() + ")");
                    }

                    this.DOWID = this.findID();
                }
                else
                {  // SAVE
                    char letter = this.dow.ToString().ToCharArray()[0];
                    dbc.executeNonQuery("UPDATE DayOfWeek SET letter='" + letter + "', name='" + this.dow.ToString() + "' WHERE dowid='" + this.DOWID + "'");

                }
                return DOWID;
            }
            catch (Exception e)
            {
                UTIL.sayDebug(e);
                throw e;
            }
        }


        public void loadData()
        {
            try
            {
                DataTable table = dbc.query("SELECT * FROM DayOfWeek WHERE dowid='" + this.DOWID + "'");
                if ((table != null) && table.Rows.Count > 0)
                {
                    foreach (DataRow row in table.Rows)
                    {
                        //this.DOWID = (int)row["dowid"];
                        this.setEnum((string)row["name"]);
                    }
                }
            }
            catch (Exception e)
            {
                UTIL.sayDebug(e);
                throw e;
            }
        }


        /// <summary>
        /// deletes from the database this specific instance
        /// returns the status of the operation
        /// </summary>
        public int removeData()
        {
            try
            {
                if (this.DOWID != -1)
                {
                    if (!isBeingUsed())
                    {
                        dbc.executeNonQuery("DELETE FROM DayOfWeek WHERE dowid='" + this.DOWID + "'");
                        return 0;
                    }
                    else return -1;
                }
                else return -1;
            }
            catch (Exception e)
            {
                UTIL.sayDebug(e);
                throw e;
            }
        }

        /// <summary>
        /// sets the enum DOW to whatever name you give it
        /// </summary>
        /// <param name="name"></param>
        public void setDayOfWeek(String name)
        {
            this.setEnum(name.Trim());
        }

        #endregion
    } // end class


} //end namespace